package com.hunttown.mes.common.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * created by hunttown on 2017/11/2
 */
public class StringUtils extends org.apache.commons.lang.StringUtils {

    public StringUtils() {
    }

    public static String trimSbc(String tarStr) {
        if (isEmpty(tarStr)) {
            return tarStr;
        } else {
            for (tarStr = tarStr.trim(); tarStr.startsWith("　"); tarStr = tarStr.substring(1, tarStr.length()).trim()) {
                ;
            }

            while (tarStr.endsWith("　")) {
                tarStr = tarStr.substring(0, tarStr.length() - 1).trim();
            }

            return tarStr;
        }
    }

    public static String change(String htmlStr) {
        if (isBlank(htmlStr)) {
            return htmlStr;
        } else {
            String temp2 = "";
            String regEx_a = "(<\\s*?(a|A)\\s*?>?[\\s\\S]*?<?[\\s]*?\\/[\\s]*?(a|A)[\\s]*?>)";
            String regEx_href = "((href|HREF)\\s*=\\s*(\"|'|)(http://)?\\w+\\.360buy\\.com[\\s\\S]*?)";
            String regEx_href_content = "(href=(\"(http:\\/\\/|\\.\\/|\\/)?[\\s\\S]*?\")|href=('(http:\\/\\/|\\.\\/|\\/)?[\\s\\S]*?'))";
            String regEx_script = "(<[\\s]*?(script|SCRIPT)[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?(script|SCRIPT)[\\s]*?>)";
            String regEx_object = "(<[\\s]*?(object|OBJECT)[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?(object|OBJECT)[\\s]*?>)";
            String regEx_frame = "(<[\\s]*?(frame|FRAME)[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?(frame|FRAME)[\\s]*?>)";
            String regEx_iframe = "(<[\\s]*?(iframe|IFRAME)[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?(iframe|IFRAME)[\\s]*?>)";
            String regEx_form = "(<[\\s]*?\\/?[\\s]*?form[^>]*?>)";
            String regEx_input = "(<[\\s]*?\\/?[\\s]*?input[^>]*?>)";
            String regEx_textarea = "(<[\\s]*?\\/?[\\s]*?textarea[^>]*?>)";
            String regEx_select = "(<[\\s]*?\\/?[\\s]*?select[^>]*?>)";
            String regEx_option = "(<[\\s]*?\\/?[\\s]*?option[^>]*?>)";
            String regEx_button = "(<[\\s]*?\\/?[\\s]*?button[^>]*?>)";
            Pattern p_script = Pattern.compile(regEx_script, 2);
            Matcher m_script = p_script.matcher(htmlStr);
            String temp = m_script.replaceAll(" ");
            Pattern p_object = Pattern.compile(regEx_object, 2);
            Matcher m_object = p_object.matcher(temp);
            temp = m_object.replaceAll(" ");
            Pattern p_frame = Pattern.compile(regEx_frame, 2);
            Matcher m_frame = p_frame.matcher(temp);
            temp = m_frame.replaceAll(" ");
            Pattern p_iframe = Pattern.compile(regEx_iframe, 2);
            Matcher m_iframe = p_iframe.matcher(temp);
            temp = m_iframe.replaceAll(" ");
            Pattern p_a = Pattern.compile(regEx_a, 2);
            Matcher m_a = p_a.matcher(temp);
            Pattern p_form = Pattern.compile(regEx_form, 2);
            Matcher m_form = p_form.matcher(temp);
            temp = m_form.replaceAll(" ");
            Pattern p_input = Pattern.compile(regEx_input, 2);
            Matcher m_input = p_input.matcher(temp);
            temp = m_input.replaceAll(" ");
            Pattern p_textarea = Pattern.compile(regEx_textarea, 2);
            Matcher m_textarea = p_textarea.matcher(temp);
            temp = m_textarea.replaceAll(" ");
            Pattern p_select = Pattern.compile(regEx_select, 2);
            Matcher m_select = p_select.matcher(temp);
            temp = m_select.replaceAll(" ");
            Pattern p_option = Pattern.compile(regEx_option, 2);
            Matcher m_option = p_option.matcher(temp);
            temp = m_option.replaceAll(" ");
            Pattern p_button = Pattern.compile(regEx_button, 2);
            Matcher m_button = p_button.matcher(temp);
            temp = m_button.replaceAll(" ");
            temp2 = temp;
            Pattern p_href = Pattern.compile(regEx_href, 2);

            while (true) {
                String sb;
                Matcher m_href;
                do {
                    if (!m_a.find()) {
                        return temp2;
                    }

                    sb = null;
                    sb = m_a.group(0);
                    m_href = p_href.matcher(sb);
                } while (m_href.find());

                Pattern p_content = Pattern.compile(regEx_href_content, 2);

                String change;
                for (Matcher m_content = p_content.matcher(sb); m_content.find(); temp2 = stringReplace(temp2, change, "href=\"#\"")) {
                    change = null;
                    change = m_content.group(0);
                }
            }
        }
    }

    public static String stringReplace(String sourceString, String toReplaceString, String replaceString) {
        String returnString = sourceString;
        int stringLength = 0;
        if (toReplaceString != null) {
            stringLength = toReplaceString.length();
        }

        if (sourceString != null && sourceString.length() >= stringLength) {
            int max = 0;
            String S4 = " ";

            for (int i = 0; i < sourceString.length(); ++i) {
                max = i + toReplaceString.length() > sourceString.length() ? sourceString.length() : i + stringLength;
                String S3 = sourceString.substring(i, max);
                if (!S3.equals(toReplaceString)) {
                    S4 = S4 + S3.substring(0, 1);
                } else {
                    S4 = S4 + replaceString;
                    i += stringLength - 1;
                }
            }

            returnString = S4;
        }

        return returnString;
    }

    /**
     * 不够位数的在前面补0，保留num的长度位数字
     *
     * @param code 要补全的数
     * @param num  保留num的位数
     * @return 补全后的数
     */
    public static String padLeft(Integer code, int num) {
        // 0    代表前面补充0
        // num  代表长度为4
        // d    代表参数为正数型
        return String.format("%0" + num + "d", code);
    }

    /**
     * 不够位数的在前面补0，保留num的长度位数字
     *
     * @param code 要补全的数
     * @param num  保留num的位数
     * @return 补全后的数
     */
    public static String padLeft(String code, int num) {
        return padLeft(Integer.valueOf(code), num);
    }

    /**
     * 数据截断
     *
     * @param origin 字符串
     * @param len    长度
     * @return 截断后的字符串
     */
    public static String textCut(String origin, Integer len) {
        if (isBlank(origin)) {
            return origin;
        }

        if (origin.length() <= len) {
            return origin;
        }

        origin = origin.substring(0, len);
        return origin + " ......";
    }

    /**
     * 判断整型是否【无效】
     *
     * @param number 要判断的数字
     * @return TF
     */
    public static boolean intIsBlank(Integer number) {
        return number == null || number <= 0;
    }

    /**
     * 判断整型是否【有效】
     *
     * @param number 要判断的数字
     * @return TF
     */
    public static boolean intIsNotBlank(Integer number) {
        return number != null && number > 0;
    }
}
